#include "seatparameterdialog.h"
#include "ui_seatparameterdialog.h"
#include <QTreeWidgetItem>
#include <QWidgetAction>
#include <QFileDialog>
#include <QTableWidgetItem>
#include <QMessageBox>
#include <QSerialPortInfo>
#include <QMenu>
//#include "../mainwindow.h"
#include "CameraDialog.h"
#include "classdialog.h"
#include "newdevicedialog.h"
#include "newoperationdialog.h"

#include "../description/rs232device.h"
#include "../description/JakaRobotDevice.h"
#include "../description/DahengCameraDevice.h"
#include "../description/RecognizeDevice.h"

#include "../description/DelayOperation.h"
#include "../description/WordopControlLEDOperation.h"
#include "../description/MoveJointOperation.h"
#include "../description/SnapOperation.h"
#include "../description/RecognizeOperation.h"

SeatParameterDialog::SeatParameterDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::SeatParameterDialog)
{
    ui->setupUi(this);

    InitComboBoxDatas();

    InitComboBoxAllDeviceDatas();

    InitCameraInfoDatas();

    InitTreeWidget();

    InitControls();

    ui->stackedWidget->setCurrentWidget(ui->pageEmpty);
}

SeatParameterDialog::SeatParameterDialog(SeatDetection& seatDetection,QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::SeatParameterDialog)
{
    ui->setupUi(this);

    m_objSeatDetection = seatDetection;

    InitComboBoxDatas();

    InitComboBoxAllDeviceDatas();

    InitCameraInfoDatas();

    InitTreeWidget();

    InitControls();
}

SeatParameterDialog::~SeatParameterDialog()
{
    delete ui;
}

void SeatParameterDialog::InitComboBoxDatas(){
    // (2) 串口号，获取所有可用的串口信息
    QList<QSerialPortInfo> serialPorts = QSerialPortInfo::availablePorts();
    ui->comboBoxPort->clear();
    // 输出串口信息

    //qDebug() << "Available Serial Ports:";
    for (const QSerialPortInfo &serialPort : serialPorts) {
        //qDebug() << "Port: " << serialPort.portName() << "Description: " << serialPort.description();
        QString strSerialPort = QString("%1 %2").arg(serialPort.portName()).arg(serialPort.description());
        ui->comboBoxPort->addItem(strSerialPort);
    }
    // (3) 波特率
    ui->comboBoxBaudrate->addItem(QString::number(QSerialPort::BaudRate::Baud115200));
    ui->comboBoxBaudrate->addItem(QString::number(QSerialPort::BaudRate::Baud57600));
    ui->comboBoxBaudrate->addItem(QString::number(QSerialPort::BaudRate::Baud38400));
    ui->comboBoxBaudrate->addItem(QString::number(QSerialPort::BaudRate::Baud19200));
    ui->comboBoxBaudrate->addItem(QString::number(QSerialPort::BaudRate::Baud9600));
    ui->comboBoxBaudrate->addItem(QString::number(QSerialPort::BaudRate::Baud4800));
    ui->comboBoxBaudrate->addItem(QString::number(QSerialPort::BaudRate::Baud2400));
    ui->comboBoxBaudrate->addItem(QString::number(QSerialPort::BaudRate::Baud1200));

    // (4) 数据位
    ui->comboBoxDatabits->addItem(QString::number(QSerialPort::DataBits::Data5));
    ui->comboBoxDatabits->addItem(QString::number(QSerialPort::DataBits::Data6));
    ui->comboBoxDatabits->addItem(QString::number(QSerialPort::DataBits::Data7));
    ui->comboBoxDatabits->addItem(QString::number(QSerialPort::DataBits::Data8));

    // (5) 停止位
    ui->comboBoxStopbits->addItem("1 位",QVariant::fromValue(QSerialPort::StopBits::OneStop));
    ui->comboBoxStopbits->addItem("1.5 位",QVariant::fromValue(QSerialPort::StopBits::OneAndHalfStop));
    ui->comboBoxStopbits->addItem("2 位",QVariant::fromValue(QSerialPort::StopBits::TwoStop));

    // (6) 奇偶校验
    ui->comboBoxParity->addItem("无奇偶校验",QVariant::fromValue(QSerialPort::Parity::NoParity));
    ui->comboBoxParity->addItem("偶校验",QVariant::fromValue(QSerialPort::Parity::EvenParity));
    ui->comboBoxParity->addItem("奇校验",QVariant::fromValue(QSerialPort::Parity::OddParity));
    ui->comboBoxParity->addItem("Space",QVariant::fromValue(QSerialPort::Parity::SpaceParity));
    ui->comboBoxParity->addItem("Mark",QVariant::fromValue(QSerialPort::Parity::MarkParity));

    // (7) 流控制
    ui->comboBoxFlowcontrol->addItem("无流控制",QVariant::fromValue(QSerialPort::FlowControl::NoFlowControl));
    ui->comboBoxFlowcontrol->addItem("硬件流控制",QVariant::fromValue(QSerialPort::FlowControl::HardwareControl));
    ui->comboBoxFlowcontrol->addItem("软件流控制",QVariant::fromValue(QSerialPort::FlowControl::SoftwareControl));
    // (8) 是否使用GPU
    ui->comboBoxUseGPU->addItem("True");
    ui->comboBoxUseGPU->addItem("False");

    // comboBoxAllDevices

    for(int i = 0;i < 4;i++){
        ui->comboBoxLEDChannel->addItem(QString::number(i));
    }
}

void SeatParameterDialog::InitComboBoxAllDeviceDatas(){
    disconnect(ui->comboBoxLEDDevices, &QComboBox::currentTextChanged, this, &SeatParameterDialog::onLEDDevicesTextChanged);
    disconnect(ui->comboBoxSnapDevices, &QComboBox::currentTextChanged, this, &SeatParameterDialog::onSnapDevicesTextChanged);
    disconnect(ui->comboBoxRecognizeDevices, &QComboBox::currentTextChanged, this, &SeatParameterDialog::onRecognizeDevicesTextChanged);
    disconnect(ui->comboBoxMoveJointDevices, &QComboBox::currentTextChanged, this, &SeatParameterDialog::onMovelJointDevicesTextChanged);

    ui->comboBoxLEDDevices->clear();
    ui->comboBoxMoveJointDevices->clear();
    ui->comboBoxSnapDevices->clear();
    ui->comboBoxRecognizeDevices->clear();

    QList<Device *> pDevices = m_objSeatDetection.GetDescription().GetDevices();
    QSet<QString> strIDs;

    for(int i = 0;i < pDevices.size();i++){
        Device * pDevice = pDevices.at(i);
        strIDs.insert(pDevice->getStrID());
    }

    //qSort(strIDs.begin(), strIDs.end());

    for(QString strID : strIDs){
        ui->comboBoxLEDDevices->addItem(strID);
        ui->comboBoxMoveJointDevices->addItem(strID);
        ui->comboBoxSnapDevices->addItem(strID);
        ui->comboBoxRecognizeDevices->addItem(strID);
    }

    connect(ui->comboBoxLEDDevices, &QComboBox::currentTextChanged, this, &SeatParameterDialog::onLEDDevicesTextChanged);
    connect(ui->comboBoxSnapDevices, &QComboBox::currentTextChanged, this, &SeatParameterDialog::onSnapDevicesTextChanged);
    connect(ui->comboBoxRecognizeDevices, &QComboBox::currentTextChanged, this, &SeatParameterDialog::onRecognizeDevicesTextChanged);
    connect(ui->comboBoxMoveJointDevices, &QComboBox::currentTextChanged, this, &SeatParameterDialog::onMovelJointDevicesTextChanged);
}

void SeatParameterDialog::InitCameraInfoDatas(){
    disconnect(ui->comboBoxCameras, &QComboBox::currentTextChanged, this, &SeatParameterDialog::onCamerasTextChanged);
    ui->comboBoxCameras->clear();

    QVector<CameraInfo> camerainfos = m_objSeatDetection.GetCameraInfos();
    for (int var = 0; var < camerainfos.count(); ++var) {
        CameraInfo ci = camerainfos.at(var);
        ui->comboBoxCameras->addItem(ci.GetName());
    }

    disconnect(ui->comboBoxCameras, &QComboBox::currentTextChanged, this, &SeatParameterDialog::onCamerasTextChanged);
}

void SeatParameterDialog::InitControls(){
    // (1) Base
    QAction *pTrailingAction = new QAction(this);
    QIcon icon = QIcon(":/icon/res/icon/folder.png");
    pTrailingAction->setIcon(QIcon(":/icon/res/icon/folder.png"));
    ui->lineEditBaseSrcPath->addAction(pTrailingAction, QLineEdit::TrailingPosition);
    // 连接信号和槽
    connect(pTrailingAction, SIGNAL(triggered()), this, SLOT(On_Base_ImageSrc_Slots()));

    connect(ui->treeWidgetCategory,SIGNAL(currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),this,
            SLOT(On_CurrentDeviceTreeItemChanged_Slots(QTreeWidgetItem *,QTreeWidgetItem *)));

    connect(ui->listWidgetDevices,SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),this,
            SLOT(On_CurrentDeviceListItemChanged_Slots(QListWidgetItem *,QListWidgetItem *)));

    connect(ui->treeWidgetSequences,SIGNAL(currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),this,
            SLOT(On_CurrentSequenceTreeItemChanged_Slots(QTreeWidgetItem *,QTreeWidgetItem *)));

    connect(ui->spinBoxImageWidth, &QSpinBox::textChanged, this, &SeatParameterDialog::onImageWidthTextChanged);
    connect(ui->spinBoxImageHeight, &QSpinBox::textChanged, this, &SeatParameterDialog::onImageHeightTextChanged);
    connect(ui->spinBoxImageChannel, &QSpinBox::textChanged, this, &SeatParameterDialog::onImageChannelTextChanged);

    connect(ui->spinBoxPartWidth, &QSpinBox::textChanged, this, &SeatParameterDialog::onPartWidthTextChanged);
    connect(ui->spinBoxPartHeight, &QSpinBox::textChanged, this, &SeatParameterDialog::onPartHeightTextChanged);

    connect(ui->spinBoxSubwindowRow, &QSpinBox::editingFinished, this, &SeatParameterDialog::onSubwindowRowTextChanged);
    connect(ui->spinBoxSubwindowColumn, &QSpinBox::editingFinished, this, &SeatParameterDialog::onSubwindowColumnTextChanged);
    connect(ui->spinBoxSubwindowTotal, &QSpinBox::editingFinished, this, &SeatParameterDialog::onSubwindowTotalTextChanged);

    connect(ui->lineEditRS232DeviceID, &QLineEdit::textChanged, this, &SeatParameterDialog::onRS232DeviceIDTextChanged);
    connect(ui->lineEditDahengCameraDeviceID, &QLineEdit::textChanged, this, &SeatParameterDialog::onDahengCameraDeviceIDTextChanged);
    connect(ui->comboBoxCameras, &QComboBox::currentTextChanged, this, &SeatParameterDialog::onCamerasTextChanged);
    connect(ui->lineEditDahengCameraDeviceIP, &QLineEdit::textChanged, this, &SeatParameterDialog::onDahengCameraDeviceIPTextChanged);
    connect(ui->lineEditJakaRobotDeviceID, &QLineEdit::textChanged, this, &SeatParameterDialog::onJakaRobotDeviceIDTextChanged);
    connect(ui->lineEditJakaRobotDeviceIP, &QLineEdit::textChanged, this, &SeatParameterDialog::onJakaRobotDeviceIPTextChanged);
    connect(ui->lineEditJakaRobotDevicePort, &QLineEdit::textChanged, this, &SeatParameterDialog::onJakaRobotDevicePortTextChanged);

    connect(ui->lineEditRecognizeDeviceID, &QLineEdit::textChanged, this, &SeatParameterDialog::onRecognizeDeviceIDTextChanged);
    //connect(ui->lineEditRecognizeModelPath, &QLineEdit::textChanged, this, &SeatParameterDialog::onRecognizeDeviceModelPathTextChanged);
    connect(ui->comboBoxUseGPU, &QComboBox::currentTextChanged, this, &SeatParameterDialog::onRecognizeDeviceUseGPUTextChanged);

    connect(ui->lineEditDelayDuration, &QLineEdit::textChanged, this, &SeatParameterDialog::onDelayDurationTextChanged);

    connect(ui->comboBoxLEDChannel, &QComboBox::currentTextChanged, this, &SeatParameterDialog::onLEDChannelTextChanged);
    connect(ui->lineEditLEDValue, &QLineEdit::textChanged, this, &SeatParameterDialog::onLEDValueTextChanged);

    connect(ui->lineEditJoint1, &QLineEdit::textChanged, this, &SeatParameterDialog::onJoint1TextChanged);
    connect(ui->lineEditJoint2, &QLineEdit::textChanged, this, &SeatParameterDialog::onJoint2TextChanged);
    connect(ui->lineEditJoint3, &QLineEdit::textChanged, this, &SeatParameterDialog::onJoint3TextChanged);
    connect(ui->lineEditJoint4, &QLineEdit::textChanged, this, &SeatParameterDialog::onJoint4TextChanged);
    connect(ui->lineEditJoint5, &QLineEdit::textChanged, this, &SeatParameterDialog::onJoint5TextChanged);
    connect(ui->lineEditJoint6, &QLineEdit::textChanged, this, &SeatParameterDialog::onJoint6TextChanged);

    connect(ui->lineEditSnapImage, &QLineEdit::textChanged, this, &SeatParameterDialog::onSnapImageTextChanged);
    connect(ui->lineEditRecognizeImage, &QLineEdit::textChanged, this, &SeatParameterDialog::onRecognizeImageTextChanged);

    connect(ui->lineEditStepNumber, &QLineEdit::textChanged, this, &SeatParameterDialog::onStepNumberTextChanged);

    QAction *pModePathAction = new QAction(this);
    pModePathAction->setIcon(QIcon(":/icon/res/icon/folder.png"));
    ui->lineEditRecognizeModelPath->addAction(pModePathAction, QLineEdit::TrailingPosition);
    // 连接信号和槽
    connect(pModePathAction, SIGNAL(triggered()), this, SLOT(On_Recoginzer_ModelPath_Slots()));

    connect(ui->comboBoxPort, &QComboBox::currentTextChanged, this, &SeatParameterDialog::onSerialPortPortTextChanged);
    connect(ui->comboBoxBaudrate, &QComboBox::currentTextChanged, this, &SeatParameterDialog::onSerialPortBaudTextChanged);
    connect(ui->comboBoxDatabits, &QComboBox::currentTextChanged, this, &SeatParameterDialog::onSerialPortDatabitsTextChanged);
    connect(ui->comboBoxStopbits, &QComboBox::currentTextChanged, this, &SeatParameterDialog::onSerialPortStopbitsTextChanged);
    connect(ui->comboBoxParity, &QComboBox::currentTextChanged, this, &SeatParameterDialog::onSerialPortParityTextChanged);
    connect(ui->comboBoxFlowcontrol, &QComboBox::currentTextChanged, this, &SeatParameterDialog::onSerialPortFlowcontrolTextChanged);

    connect(ui->comboBoxLEDDevices, &QComboBox::currentTextChanged, this, &SeatParameterDialog::onLEDDevicesTextChanged);
    connect(ui->comboBoxSnapDevices, &QComboBox::currentTextChanged, this, &SeatParameterDialog::onSnapDevicesTextChanged);
    connect(ui->comboBoxRecognizeDevices, &QComboBox::currentTextChanged, this, &SeatParameterDialog::onRecognizeDevicesTextChanged);
    connect(ui->comboBoxMoveJointDevices, &QComboBox::currentTextChanged, this, &SeatParameterDialog::onMovelJointDevicesTextChanged);


    // 显示数据
    InitPageBase();

    InitPageCameraInfo();

    InitPageYolo();

    InitDetetingWindow();

    InitDescriptionDevices();

    InitDescriptionSequences();
}

void SeatParameterDialog::InitPageBase(){
    ui->lineEditBaseSrcPath->setText(m_objSeatDetection.GetBaseInfo().GetImageSrcPath());
}

void SeatParameterDialog::createCameraRowData(int index,QString strIP, QString strMAC, QString strName,QString strIdentifiers)
{
    // 创建一个 QList，用于存储每一行的 QTableWidgetItem 对象
    QList<QTableWidgetItem*> items;
    items.append(new QTableWidgetItem(strIP));
    items.append(new QTableWidgetItem(strMAC));
    items.append(new QTableWidgetItem(strName));
    items.append(new QTableWidgetItem(strIdentifiers));

    // 循环遍历所有列，设置单元格内容居中对齐，并将其添加到表格中
    for (int i = 0; i < items.size(); i++) {
        items[i]->setTextAlignment(Qt::AlignCenter); // 设置单元格内容居中对齐
        ui->tableWidgetCameras->setItem(index, i, items[i]); // 将单元格内容设置到表格中
    }
}

void SeatParameterDialog::updateCameraRowData(int index,QString strIP,QString strMAC,QString strName,QString strIdentifiers){
    QTableWidgetItem* pItem = new QTableWidgetItem(strIP);
    pItem->setTextAlignment(Qt::AlignCenter);
    ui->tableWidgetCameras->setItem(index,0,pItem);
    pItem = new QTableWidgetItem(strMAC);
    pItem->setTextAlignment(Qt::AlignCenter);
    ui->tableWidgetCameras->setItem(index,1,pItem);
    pItem = new QTableWidgetItem(strName);
    pItem->setTextAlignment(Qt::AlignCenter);
    ui->tableWidgetCameras->setItem(index,2,pItem);
    pItem = new QTableWidgetItem(strIdentifiers);
    pItem->setTextAlignment(Qt::AlignCenter);
    ui->tableWidgetCameras->setItem(index,3,pItem);
}

void SeatParameterDialog::InitCamerasTableData()
{
    // 定义表头内容：包括姓名、性别、出生日期、专业、是否毕业
    QStringList headList = {"IP", "MAC", "名称", "图片编号"};

    // 设置表格的列数为表头的数量
    ui->tableWidgetCameras->setColumnCount(headList.count());

    // 遍历表头列表，设置每一列的表头项
    for (int i = 0; i < headList.size(); i++) {
        // 创建一个新的 QTableWidgetItem 对象，用于存储表头文本
        auto* item = new QTableWidgetItem(headList[i]);

        // 将表头项设置到表格的水平表头中
        ui->tableWidgetCameras->setHorizontalHeaderItem(i, item);
    }

    // 获取表格的水平表头对象，并设置列宽自动伸展模式
    auto* headerView = ui->tableWidgetCameras->horizontalHeader();
    headerView->setSectionResizeMode(QHeaderView::Stretch);

    // 设置第二列（性别列）为固定宽度，不随窗口调整而变化
    //headerView->setSectionResizeMode(1, QHeaderView::Fixed);

    // 设置表格的行数为 100 行
    QVector<CameraInfo> cameraInfos = m_objSeatDetection.GetCameraInfos();
    int row = cameraInfos.size();
    ui->tableWidgetCameras->setRowCount(row);

    int maxNum = 0;
    // 循环生成 100 行数据
    for (int i = 0; i < row; i++) {
        maxNum++; // 员工编号递增
        CameraInfo cameraInfo = cameraInfos.at(i);

        // 随机生成员工信息
        QString strIP = cameraInfo.GetIPAddress();
        QString strMAC = cameraInfo.GetMACAddress();
        QString strName = cameraInfo.GetName();
        QString strIdentifiers = cameraInfo.GetCameraIdentifiers();

        // 调用 createRowData 函数，将生成的数据填充到表格中
        createCameraRowData(i,strIP,strMAC,strName,strIdentifiers);
    }
}

void SeatParameterDialog::createDetectingWindowRowData(int index, QString name, int number,
                                                       QString sex, QDate birthday, QString subject, bool graduate){
    // 创建一个 QList，用于存储每一行的 QTableWidgetItem 对象
    QList<QTableWidgetItem*> items;

    // 姓名列
    items.append(new QTableWidgetItem(name));
    items[0]->setData(Qt::UserRole, QVariant(number)); // 使用 UserRole 存储员工编号

    // 性别列
    items.append(new QTableWidgetItem(sex));
    QIcon icon((sex == "男") ? ":/image/boy.png" : ":/image/girl.png"); // 根据性别设置图标
    items[1]->setIcon(icon);

    // 出生日期列
    items.append(new QTableWidgetItem(birthday.toString("yyyy-MM-dd"))); // 格式化日期为字符串

    // 专业列
    items.append(new QTableWidgetItem(subject));

    // 是否毕业列
    items.append(new QTableWidgetItem(graduate ? "已毕业" : "未毕业"));

    // 循环遍历所有列，设置单元格内容居中对齐，并将其添加到表格中
    for (int i = 0; i < items.size(); i++) {
        items[i]->setTextAlignment(Qt::AlignCenter); // 设置单元格内容居中对齐
        ui->tableWidgetDetectingWindow->setItem(index, i, items[i]); // 将单元格内容设置到表格中
    }
}

void SeatParameterDialog::RenewCameraFaceComboBoxs(int nNewRow){
    QVector<CameraInfo>& camerainfos = m_objSeatDetection.GetCameraInfos();
    // 清除 组件对象
    QComboBox* pQComboBox;
    for(int i = m_vecCameras.count() - 1;i >= 0; i--){
        pQComboBox = m_vecCameras.at(i);
        delete pQComboBox;
        m_vecCameras.removeLast();
    }

    for(int i = m_vecFaces.count() - 1;i >= 0; i--){
        pQComboBox = m_vecFaces.at(i);
        delete pQComboBox;
        m_vecFaces.removeLast();
    }
    // 清除 TableItem对象
    int nRow = ui->tableWidgetDetectingWindow->rowCount();
    // 删除所有行
    while (ui->tableWidgetDetectingWindow->rowCount() > 0) {
        ui->tableWidgetDetectingWindow->removeRow(0);
    }

    ui->tableWidgetDetectingWindow->setRowCount(nNewRow);
    // 重新设置
    for(int i = 0; i < nNewRow; i++){
        QString imageNum = QString::asprintf("图像%d", i);
        QTableWidgetItem* pItem = new QTableWidgetItem(imageNum);
        ui->tableWidgetDetectingWindow->setItem(i,0,pItem);

        QComboBox* pcomboboxCamera = new QComboBox();
        ui->tableWidgetDetectingWindow->setCellWidget(i,1,pcomboboxCamera);
        for(int j = 0; j < camerainfos.count();j++){
            CameraInfo ci = camerainfos.at(j);
            pcomboboxCamera->addItem(ci.GetName());
        }

        m_vecCameras.append(pcomboboxCamera);

        QComboBox* pcomboboxFace = new QComboBox();
        ui->tableWidgetDetectingWindow->setCellWidget(i,2,pcomboboxFace);
        pcomboboxFace->addItem("A 面");
        pcomboboxFace->addItem("B 面");
        pcomboboxFace->addItem("C 面");
        pcomboboxFace->addItem("D 面");
        pcomboboxFace->addItem("E 面");
        pcomboboxFace->addItem("F 面");
        m_vecFaces.append(pcomboboxFace);
    }
}

void SeatParameterDialog::InitCameraFaceComboBoxs(){
    int nTotal = m_objSeatDetection.GetDetectionWindowInfo().GetImageTotal();
    ui->tableWidgetDetectingWindow->setRowCount(nTotal);

    QVector<CameraInfo>& camerainfos = m_objSeatDetection.GetCameraInfos();

    QVector<SubImageInfo*>& seatfaces = m_objSeatDetection.GetDetectionWindowInfo().GetSeatFaces();
    int index = 0;

    if(nTotal != seatfaces.count()){
        for(int i = 0 ;i < seatfaces.count(); i++){
            SubImageInfo* pSubImageInfo = seatfaces.at(i);
            delete pSubImageInfo;
        }

        seatfaces.clear();

        for(int i = 0; i < nTotal; i++){
            SubImageInfo* pSubImageInfo = new SubImageInfo();
            m_objSeatDetection.GetDetectionWindowInfo().AppendSeatFace(pSubImageInfo);
        }
    }

    for(int i = 0; i < nTotal; i++){
        QString imageNum = QString::asprintf("图像%d", i);
        QTableWidgetItem* pItem = new QTableWidgetItem(imageNum);
        ui->tableWidgetDetectingWindow->setItem(i,0,pItem);

        QComboBox* pcomboboxCamera = new QComboBox();
        SubImageInfo* pSubImageInfo = seatfaces.at(i);
        ui->tableWidgetDetectingWindow->setCellWidget(i,1,pcomboboxCamera);
        for(int j = 0; j < camerainfos.count();j++){
            CameraInfo ci = camerainfos.at(j);
            pcomboboxCamera->addItem(ci.GetName());
        }

        m_vecCameras.append(pcomboboxCamera);
        //pcomboboxCamera->setCurrentIndex(0);
        pcomboboxCamera->setCurrentText(pSubImageInfo->GetCameraName());

        QComboBox* pcomboboxFace = new QComboBox();
        ui->tableWidgetDetectingWindow->setCellWidget(i,2,pcomboboxFace);
        pcomboboxFace->addItem("A 面");
        pcomboboxFace->addItem("B 面");
        pcomboboxFace->addItem("C 面");
        pcomboboxFace->addItem("D 面");
        pcomboboxFace->addItem("E 面");
        pcomboboxFace->addItem("F 面");
        m_vecFaces.append(pcomboboxFace);
        if(seatfaces.count() <= nTotal){
            SubImageInfo* pSubImageInfo = seatfaces.at(i);
            pcomboboxFace->setCurrentText(pSubImageInfo->GetFace());

            if(seatfaces.count() <= nTotal){
                SubImageInfo* pSubImageInfo = seatfaces.at(i);
                if(pSubImageInfo->GetFace() == "A 面"){
                    index = 0;
                }else if(pSubImageInfo->GetFace() == "B 面"){
                    index = 1;
                }else if(pSubImageInfo->GetFace() == "C 面"){
                    index = 2;
                }else if(pSubImageInfo->GetFace() == "D 面"){
                    index = 3;
                }else if(pSubImageInfo->GetFace() == "E 面"){
                    index = 4;
                }else if(pSubImageInfo->GetFace() == "F 面"){
                    index = 5;
                }
            }
        }

        pcomboboxFace->setCurrentIndex(index);
    }
}

void SeatParameterDialog::InitDetectingWindowsTableData(){
    // 定义表头内容：包括姓名、性别、出生日期、专业、是否毕业
    QStringList headList = {"图像", "相机", "座椅面"};

    // 设置表格的列数为表头的数量
    ui->tableWidgetDetectingWindow->setColumnCount(headList.count());

    // 遍历表头列表，设置每一列的表头项
    for (int i = 0; i < headList.size(); i++) {
        // 创建一个新的 QTableWidgetItem 对象，用于存储表头文本
        auto* item = new QTableWidgetItem(headList[i]);

        // 将表头项设置到表格的水平表头中
        ui->tableWidgetDetectingWindow->setHorizontalHeaderItem(i, item);
    }

    // 获取表格的水平表头对象，并设置列宽自动伸展模式
    auto* headerView = ui->tableWidgetDetectingWindow->horizontalHeader();
    headerView->setSectionResizeMode(QHeaderView::Stretch);

    InitCameraFaceComboBoxs();
}

void SeatParameterDialog::InitPageCameraInfo(){
    ui->spinBoxImageWidth->setValue(m_objSeatDetection.GetImageInfo().GetImageWidth());
    ui->spinBoxImageHeight->setValue(m_objSeatDetection.GetImageInfo().GetImageHeight());
    ui->spinBoxImageChannel->setValue(m_objSeatDetection.GetImageInfo().GetImageChannel());

    InitCamerasTableData();
}

void SeatParameterDialog::InitYoloClassesTableData(){
    // 定义表头内容：包括姓名、性别、出生日期、专业、是否毕业
    QStringList headList = {"序号","类别"};

    // 设置表格的列数为表头的数量
    ui->tableWidgetClasses->setColumnCount(headList.count());

    // 遍历表头列表，设置每一列的表头项
    for (int i = 0; i < headList.size(); i++) {
        // 创建一个新的 QTableWidgetItem 对象，用于存储表头文本
        auto* item = new QTableWidgetItem(headList[i]);

        // 将表头项设置到表格的水平表头中
        ui->tableWidgetClasses->setHorizontalHeaderItem(i, item);
    }

    // 获取表格的水平表头对象，并设置列宽自动伸展模式
    auto* headerView = ui->tableWidgetClasses->horizontalHeader();
    headerView->setSectionResizeMode(QHeaderView::Stretch);

    QVector<CategoryInfo> categoryInfos = m_objSeatDetection.GetCategoryInfos();
    int row = categoryInfos.size();
    ui->tableWidgetClasses->setRowCount(row);

    for (int i = 0; i < row; i++) {
        CategoryInfo categoryInfo = categoryInfos.at(i);

        int nKey            = categoryInfo.GetKey();
        QString strName     = categoryInfo.GetName();

        QTableWidgetItem* item = new QTableWidgetItem(QString::number(nKey));
        item->setTextAlignment(Qt::AlignCenter);
        ui->tableWidgetClasses->setItem(i, 0, item);

        item = new QTableWidgetItem(strName);
        item->setTextAlignment(Qt::AlignCenter);
        ui->tableWidgetClasses->setItem(i, 1, item);
    }
}

void SeatParameterDialog::InitPageYolo(){
    //ui->lineEditYoloModelPath->setText(m_objSeatDetection.GetCnnInfo().GetONNXModelPath());
    ui->spinBoxPartWidth->setValue(m_objSeatDetection.GetCnnInfo().GetPartWidth());
    ui->spinBoxPartHeight->setValue(m_objSeatDetection.GetCnnInfo().GetPartHeight());

    InitYoloClassesTableData();
}

void SeatParameterDialog::InitPagePaddleOCR(){}

void SeatParameterDialog::InitDetetingWindow(){
    ui->spinBoxSubwindowRow->setValue(m_objSeatDetection.GetDetectionWindowInfo().GetImageRow());
    ui->spinBoxSubwindowColumn->setValue(m_objSeatDetection.GetDetectionWindowInfo().GetImageCol());
    ui->spinBoxSubwindowTotal->setValue(m_objSeatDetection.GetDetectionWindowInfo().GetImageTotal());

    InitDetectingWindowsTableData();
}

void SeatParameterDialog::InitDescriptionSequences(){
    ui->treeWidgetSequences->setHeaderLabel(QStringLiteral("操作步骤"));
    QIcon iconSequence,iconStep,iconOperation;
    iconSequence.addPixmap(QPixmap(":/icon/res/icon/sequence.png"), QIcon::Normal);
    iconStep.addPixmap(QPixmap(":/icon/res/icon/step.png"), QIcon::Normal);
    iconOperation.addPixmap(QPixmap(":/icon/res/icon/operation.png"), QIcon::Normal);

    QList<Sequence *> pSequences = m_objSeatDetection.GetDescription().GetSequences();
    for(int i = 0;i < pSequences.size();i++){
        Sequence * pSequence = pSequences.at(i);

        QTreeWidgetItem *sequenceItem = new QTreeWidgetItem(QStringList() << ("Sequence"),QTreeWidgetItem::ItemType::UserType);
        ui->treeWidgetSequences->addTopLevelItem(sequenceItem);
        sequenceItem->setData(0,Qt::UserRole,QVariant::fromValue(pSequence));
        sequenceItem->setIcon(0,iconSequence);

        QList<Step *> pSteps = pSequence->GetSteps();
        for(int i = 0 ;i < pSteps.size();i ++){
            Step* pStep = pSteps.at(i);

            QTreeWidgetItem *stepItem = new QTreeWidgetItem(QStringList() << ("Step") ,QTreeWidgetItem::ItemType::UserType);
            sequenceItem->addChild(stepItem);
            stepItem->setData(0,Qt::UserRole,QVariant::fromValue(pStep));
            stepItem->setIcon(0,iconStep);

            QList<Operation *> pOperations = pStep->pOperations();
            for(int j = 0; j < pOperations.size();j++){
                Operation * pOperation = pOperations.at(j);

                QTreeWidgetItem *operationItem = new QTreeWidgetItem(QStringList() << pOperation->getClassName(),QTreeWidgetItem::ItemType::UserType);
                operationItem->setIcon(0,iconOperation);
                operationItem->setData(0,Qt::UserRole,QVariant::fromValue(pOperation));
                stepItem->addChild(operationItem);
            }
        }
    }

    connect(ui->treeWidgetSequences, &QTreeWidget::itemPressed, this, &SeatParameterDialog::OnTreeSequenceRightButtonClicked);

    ui->treeWidgetSequences->expandAll();
}

void SeatParameterDialog::InitTreeWidget(){
    ui->treeWidgetCategory->header()->setVisible(false);
    //(1) 系统
    QTreeWidgetItem *rootItem = new QTreeWidgetItem(QStringList() << "系统",QTreeWidgetItem::ItemType::Type);
    ui->treeWidgetCategory->addTopLevelItem(rootItem);
    QTreeWidgetItem *childItem = new QTreeWidgetItem(QStringList() << "基本",QTreeWidgetItem::ItemType::UserType);
    rootItem->addChild(childItem);
    //(2) 相机
    rootItem = new QTreeWidgetItem(QStringList() << "相机",QTreeWidgetItem::ItemType::Type);
    ui->treeWidgetCategory->addTopLevelItem(rootItem);
    childItem = new QTreeWidgetItem(QStringList() << "相机列表",QTreeWidgetItem::ItemType::UserType);
    rootItem->addChild(childItem);
    //(3) 模型
    rootItem = new QTreeWidgetItem(QStringList() << "模型",QTreeWidgetItem::ItemType::Type);
    ui->treeWidgetCategory->addTopLevelItem(rootItem);
    childItem = new QTreeWidgetItem(QStringList() << "YOLO模型",QTreeWidgetItem::ItemType::UserType);
    rootItem->addChild(childItem);
    childItem = new QTreeWidgetItem(QStringList() << "PaddleOCR模型",QTreeWidgetItem::ItemType::UserType);
    rootItem->addChild(childItem);
    //(4) 检测
    rootItem = new QTreeWidgetItem(QStringList() << "检测",QTreeWidgetItem::ItemType::Type);
    ui->treeWidgetCategory->addTopLevelItem(rootItem);
    childItem = new QTreeWidgetItem(QStringList() << "显示窗口",QTreeWidgetItem::ItemType::UserType);
    rootItem->addChild(childItem);
    //(5)设备
    rootItem = new QTreeWidgetItem(QStringList() << "设备",QTreeWidgetItem::ItemType::Type);
    ui->treeWidgetCategory->addTopLevelItem(rootItem);
    childItem = new QTreeWidgetItem(QStringList() << "设备配置",QTreeWidgetItem::ItemType::UserType);
    rootItem->addChild(childItem);
    //(6) 操作
    rootItem = new QTreeWidgetItem(QStringList() << "操作",QTreeWidgetItem::ItemType::Type);
    ui->treeWidgetCategory->addTopLevelItem(rootItem);
    childItem = new QTreeWidgetItem(QStringList() << "操作配置",QTreeWidgetItem::ItemType::UserType);
    rootItem->addChild(childItem);

    ui->treeWidgetCategory->expandAll();
}

void SeatParameterDialog::InitDescriptionDevices(){
    QIcon iconDevice;
    iconDevice.addPixmap(QPixmap(":/icon/res/icon/device.png"), QIcon::Normal);

    QList<Device *> pDevices = m_objSeatDetection.GetDescription().GetDevices();
    for(int i = 0 ;i < pDevices.size();i ++){
        QListWidgetItem* pItem = new QListWidgetItem(pDevices[i]->getClassName());
        pItem->setData(Qt::UserRole,QVariant::fromValue(pDevices[i]));
        pItem->setIcon(iconDevice);
        ui->listWidgetDevices->addItem(pItem);
    }
}

void SeatParameterDialog::On_CurrentDeviceTreeItemChanged_Slots(QTreeWidgetItem *current, QTreeWidgetItem *previous){
    if(current != NULL){
        qDebug()<<current->text(0);

        if(current->type() == QTreeWidgetItem::ItemType::UserType){
            if(current->text(0) == QStringLiteral("基本")){
                ui->stackedWidget->setCurrentWidget(ui->pageBase);
            }else if(current->text(0) == QStringLiteral("相机列表")){
                ui->stackedWidget->setCurrentWidget(ui->pageCameraInfo);
            }else if(current->text(0) == QStringLiteral("YOLO模型")){
                ui->stackedWidget->setCurrentWidget(ui->pageYolo);
            }else if(current->text(0) == QStringLiteral("PaddleOCR模型")){
                ui->stackedWidget->setCurrentWidget(ui->pagePaddleOCR);
            }else if(current->text(0) == QStringLiteral("显示窗口")){
                ui->stackedWidget->setCurrentWidget(ui->pageDetectingWindow);
            }else if(current->text(0) == QStringLiteral("设备配置")){
                ui->stackedWidget->setCurrentWidget(ui->pageDevices);

                ui->stackedWidgetDevices->setCurrentWidget(ui->pageEmptyDevice);
            }else if(current->text(0) == QStringLiteral("操作配置")){
                ui->stackedWidget->setCurrentWidget(ui->pageSteps);

                ui->stackedWidgetSequences->setCurrentWidget(ui->pageEmptyOperation);
            }
        }
    }
}

void SeatParameterDialog::On_CurrentSequenceTreeItemChanged_Slots(QTreeWidgetItem *current, QTreeWidgetItem *previous){
    if(current != NULL){
        qDebug()<<current->text(0);

        m_pCurrentOperation = current->data(0,Qt::UserRole).value<Operation*>();

        m_eCurrentNodeType = NodeType::OperationType;

        if(current->type() == QTreeWidgetItem::ItemType::UserType){
            if(current->text(0) == QStringLiteral("DelayOperation")){
                ui->stackedWidgetSequences->setCurrentWidget(ui->pageDelayOperation);
                int d = ((DelayOperation*)m_pCurrentOperation)->GetDuration();
                ui->lineEditDelayDuration->setText(QString::number(d));
            }else if(current->text(0) == QStringLiteral("WordopControlLEDOperation")){
                ui->stackedWidgetSequences->setCurrentWidget(ui->pageWordopControlLEDOperation);

                int channel = ((WordopControlLEDOperation*)m_pCurrentOperation)->GetChannel();
                int value = ((WordopControlLEDOperation*)m_pCurrentOperation)->GetLEDValue();

                ui->comboBoxLEDChannel->setCurrentIndex(channel);
                ui->lineEditLEDValue->setText(QString::number(value));

                ui->comboBoxLEDDevices->setCurrentText(m_pCurrentOperation->GetDeviceId());
            }else if(current->text(0) == QStringLiteral("MoveJointOperation")){
                ui->stackedWidgetSequences->setCurrentWidget(ui->pageMoveJointOperation);

                QList<double> angles = ((MoveJointOperation*)m_pCurrentOperation)->GetJointAngles();

                ui->lineEditJoint1->setText(QString::number(angles.at(0)));
                ui->lineEditJoint2->setText(QString::number(angles.at(1)));
                ui->lineEditJoint3->setText(QString::number(angles.at(2)));
                ui->lineEditJoint4->setText(QString::number(angles.at(3)));
                ui->lineEditJoint5->setText(QString::number(angles.at(4)));
                ui->lineEditJoint6->setText(QString::number(angles.at(5)));
                ui->comboBoxMoveJointDevices->setCurrentText(m_pCurrentOperation->GetDeviceId());
            }else if(current->text(0) == QStringLiteral("RecognizeOperation")){
                ui->stackedWidgetSequences->setCurrentWidget(ui->pageRecognizeOperation);
                int nImage = ((RecognizeOperation*)m_pCurrentOperation)->nImageNum();
                ui->lineEditRecognizeImage->setText(QString::number(nImage));
                ui->comboBoxRecognizeDevices->setCurrentText(m_pCurrentOperation->GetDeviceId());
            }else if(current->text(0) == QStringLiteral("SnapOperation")){
                ui->stackedWidgetSequences->setCurrentWidget(ui->pageSnapOperation);
                int nImage = ((SnapOperation*)m_pCurrentOperation)->nImageNum();
                ui->lineEditSnapImage->setText(QString::number(nImage));
                ui->comboBoxSnapDevices->setCurrentText(m_pCurrentOperation->GetDeviceId());
            }else if(current->text(0) == QStringLiteral("Step")){
                ui->stackedWidgetSequences->setCurrentWidget(ui->pageStep);

                m_pCurrentStep = current->data(0,Qt::UserRole).value<Step*>();
                QString strID = m_pCurrentStep->strID();
                ui->lineEditStepNumber->setText(strID);

                m_eCurrentNodeType = NodeType::StepType;
            }else if(current->text(0) == QStringLiteral("Sequence")){
                m_pCurrentSequence = current->data(0,Qt::UserRole).value<Sequence*>();
                m_eCurrentNodeType = NodeType::SequenceType;
            }
        }
    }
}

void SeatParameterDialog::On_CurrentDeviceListItemChanged_Slots(QListWidgetItem* current, QListWidgetItem* previous){
    if(current != NULL){
        m_pCurrentDevice = current->data(Qt::UserRole).value<Device*>();

        if(m_pCurrentDevice->getClassName() == "RS232Device"){
            //qDebug() << m_pCurrentDevice->getStrID() << m_pCurrentDevice->getClassName();
            ui->stackedWidgetDevices->setCurrentWidget(ui->pageRS232Device);
            //RS232Device* pRS232Device = (RS232Device*)m_pCurrentDevice;
            //qDebug() << pRS232Device->GetPort();
            // (1) ID
            ui->lineEditRS232DeviceID->setText(m_pCurrentDevice->strID());

            // (2) Port
            int nPort = ((RS232Device*)m_pCurrentDevice)->GetSPort();
            for(int i = 0 ;i < ui->comboBoxPort->count();i++){
                QString strContent = ui->comboBoxPort->itemText(i);
                if(strContent.contains(QString("COM%1").arg(nPort))){
                    ui->comboBoxPort->setCurrentIndex(i);
                    break;
                }
            }

            // (3) 波特率
            int count = ui->comboBoxBaudrate->count();
            QSerialPort::BaudRate br = ((RS232Device*)m_pCurrentDevice)->GetBaud();
            for(int i = 0; i < count; i++){
                QString itemText = ui->comboBoxBaudrate->itemText(i);
                if(itemText == QString::number(br)){
                    ui->comboBoxBaudrate->setCurrentIndex(i);
                    break;
                }
            }
            // (4) 数据位
            count = ui->comboBoxBaudrate->count();
            QSerialPort::DataBits db = ((RS232Device*)m_pCurrentDevice)->GetDatabits();
            for(int i = 0; i < count; i++){
                QString itemText = ui->comboBoxDatabits->itemText(i);
                if(itemText == QString::number(db)){
                    ui->comboBoxDatabits->setCurrentIndex(i);
                    break;
                }
            }
            // (5) 停止位
            count = ui->comboBoxStopbits->count();
            QSerialPort::StopBits sb = ((RS232Device*)m_pCurrentDevice)->GetStopbits();
            for(int i = 0; i < count; i++){
                QString itemText = ui->comboBoxStopbits->itemText(i);
                int value = ui->comboBoxStopbits->itemData(i).toInt();
                if(QString::number(value) == QString::number(sb)){
                    ui->comboBoxStopbits->setCurrentIndex(i);
                    break;
                }
            }
            // (6) 奇偶位
            count = ui->comboBoxParity->count();
            QSerialPort::Parity parity = ((RS232Device*)m_pCurrentDevice)->GetParity();
            for(int i = 0; i < count; i++){
                QString itemText = ui->comboBoxParity->itemText(i);
                int value = ui->comboBoxParity->itemData(i).toInt();
                if(QString::number(value) == QString::number(parity)){
                    ui->comboBoxParity->setCurrentIndex(i);
                    break;
                }
            }
        }else if(m_pCurrentDevice->getClassName() == "DahengCameraDevice"){
            ui->stackedWidgetDevices->setCurrentWidget(ui->pageDahengCameraDevice);

            DahengCameraDevice* pDahengCameraDevice = (DahengCameraDevice*)m_pCurrentDevice;
            // (1) ID
            ui->lineEditDahengCameraDeviceID->setText(pDahengCameraDevice->strID());
            // (2) IP
            ui->lineEditDahengCameraDeviceIP->setText(pDahengCameraDevice->GetIP());
            // (3) 相机

            QVector<CameraInfo> camerainfos = m_objSeatDetection.GetCameraInfos();
            CameraInfo ci;
            for(int i = 0;i < camerainfos.count();i++){
                ci = camerainfos.at(i);
                if(pDahengCameraDevice->GetIP() == ci.GetIPAddress()){
                    break;
                }
            }

            ui->comboBoxCameras->setCurrentText(ci.GetName());

        }else if(m_pCurrentDevice->getClassName() == "JakaRobotDevice"){
            qDebug() << m_pCurrentDevice->getStrID() << m_pCurrentDevice->getClassName();
            ui->stackedWidgetDevices->setCurrentWidget(ui->pageJakaRobotDevice);
            JakaRobotDevice* pJakaRobotDevice = (JakaRobotDevice*)m_pCurrentDevice;
            // (1) ID
            ui->lineEditJakaRobotDeviceID->setText(pJakaRobotDevice->strID());
            // (2) IP
            ui->lineEditJakaRobotDeviceIP->setText(pJakaRobotDevice->GetIP());
            // (3) Port
            ui->lineEditJakaRobotDevicePort->setText(QString::number(pJakaRobotDevice->GetPort_()));
        }else if(m_pCurrentDevice->getClassName() == "RecognizeDevice"){
            qDebug() << m_pCurrentDevice->getStrID() << m_pCurrentDevice->getClassName();
            ui->stackedWidgetDevices->setCurrentWidget(ui->pageRecognizeDevice);
            RecognizeDevice* pRecognizeDevice = (RecognizeDevice*)m_pCurrentDevice;
            // (1) ID
            ui->lineEditRecognizeDeviceID->setText(pRecognizeDevice->strID());
            // (2) Model Path
            ui->lineEditRecognizeModelPath->setText(pRecognizeDevice->GetModelPath());
            // (3) use GPU??

            if(pRecognizeDevice->GetUseGPU()){
                ui->comboBoxUseGPU->setCurrentIndex(0);
            }else{
                ui->comboBoxUseGPU->setCurrentIndex(1);
            }
        }
    }
}

void SeatParameterDialog::On_Base_ImageSrc_Slots(){
    QString directory = QFileDialog::getExistingDirectory(nullptr,
                                                          tr("选择文件夹"),
                                                          "c:/",
                                                          QFileDialog::ShowDirsOnly
                                                        | QFileDialog::DontResolveSymlinks);

    if (!directory.isEmpty()) {
        ui->lineEditBaseSrcPath->setText(directory);
    }
}

void SeatParameterDialog::On_Recoginzer_ModelPath_Slots(){
    QString fileName = QFileDialog::getOpenFileName(
        this, "选择Yolo模型文件", QDir::currentPath(),
        "文本文件 (*.onnx);;所有文件 (*.*)"
        );

    if (!fileName.isEmpty()) {
        ui->lineEditRecognizeModelPath->setText(fileName);
        ((RecognizeDevice*)m_pCurrentDevice)->SetModePath(fileName);
    }
}

void SeatParameterDialog::SwapRows(QTableWidget *tableWidget, int row1, int row2){
    // 确保行索引有效
    if (row1 < 0 || row1 >= tableWidget->rowCount() || row2 < 0 || row2 >= tableWidget->rowCount()) {
        return;
    }

    // 交换两行的所有项
    for (int col = 0; col < tableWidget->columnCount(); ++col) {
        QTableWidgetItem *item1 = tableWidget->takeItem(row1, col);
        QTableWidgetItem *item2 = tableWidget->takeItem(row2, col);
        if (item1) tableWidget->setItem(row2, col, item1);
        if (item2) tableWidget->setItem(row1, col, item2);
    }
}

void SeatParameterDialog::MoveUpRow(QTableWidget *tableWidget){
    m_CurrentRow = tableWidget->currentRow();//获取当前行的Index
    if (m_CurrentRow != -1)//如果选中了数据，（未选择数据之前，返回值是-1）
    {
        if (m_CurrentRow != 0)//如果选定行不在第一行
        {
            SwapRows(tableWidget, m_CurrentRow,m_CurrentRow - 1);
            tableWidget->setCurrentCell(m_CurrentRow - 1, QItemSelectionModel::Select);//移动过后继续选定该行
        }
    }
}

void SeatParameterDialog::MoveDownRow(QTableWidget *tableWidget){
    m_CurrentRow = tableWidget->currentRow();//获取当前行的Index
    int lastRow = tableWidget->rowCount() - 1;
    if (m_CurrentRow != -1)//如果选中了数据，（未选择数据之前，返回值是-1）
    {
        if (m_CurrentRow != lastRow)//如果选定行不在最后一行
        {
            SwapRows(tableWidget, m_CurrentRow,m_CurrentRow + 1);
            tableWidget->setCurrentCell(m_CurrentRow + 1, QItemSelectionModel::Select);//移动过后继续选定该行            
        }
    }
}

/**
 * @brief SeatParameterDialog::on_pushButtonAddCamera_clicked
 * 添加一个相机
 */
void SeatParameterDialog::on_pushButtonAddCamera_clicked()
{
    CameraDialog* pCameraDialog = new CameraDialog(this);
    CameraInfo cameraInfo;
    pCameraDialog->SetCameraInfo(&cameraInfo);

    if(QDialog::Accepted == pCameraDialog->exec()){
        ui->tableWidgetCameras->insertRow(ui->tableWidgetCameras->rowCount());
        createCameraRowData(ui->tableWidgetCameras->rowCount() - 1,
                            cameraInfo.GetIPAddress(),
                            cameraInfo.GetMACAddress(),
                            cameraInfo.GetName(),
                            cameraInfo.GetCameraIdentifiers());

        //m_objSeatDetection.AppendCameraInfo(cameraInfo);
    }

    delete pCameraDialog;

    InitCameraInfoDatas();
}
/**
 * @brief SeatParameterDialog::on_pushButtonDeleteCamera_clicked
 * 删除一个相机
 */
void SeatParameterDialog::on_pushButtonDeleteCamera_clicked()
{
    int currentRow = -1;
    // 获取当前选中的行
    QModelIndexList selectedIndexes = ui->tableWidgetCameras->selectionModel()->selectedIndexes();
    if (!selectedIndexes.isEmpty()) {
        currentRow = selectedIndexes.first().row();

        ui->tableWidgetCameras->removeRow(currentRow);

        //m_objSeatDetection.RemoveCameraInfo(currentRow);
    }

    InitCameraInfoDatas();
}
/**
 * @brief SeatParameterDialog::on_pushButtonModifyCamera_clicked
 * 修改一个相机的信息
 */
void SeatParameterDialog::on_pushButtonModifyCamera_clicked()
{
    int m_CurrentRow = ui->tableWidgetCameras->currentRow();//获取当前行的Index
    if(m_CurrentRow != -1){
        QTableWidgetItem *itemIP = ui->tableWidgetCameras->item(m_CurrentRow, 0);
        QTableWidgetItem *itemMAC = ui->tableWidgetCameras->item(m_CurrentRow, 1);
        QTableWidgetItem *itemName = ui->tableWidgetCameras->item(m_CurrentRow, 2);
        QTableWidgetItem *itemNumbers = ui->tableWidgetCameras->item(m_CurrentRow, 3);

        CameraDialog* pCameraDialog = new CameraDialog(this);
        CameraInfo cameraInfo;
        cameraInfo.SetIPAddress(itemIP->text());
        cameraInfo.SetMACAddress(itemMAC->text());
        cameraInfo.SetName(itemName->text());
        cameraInfo.SetCameraIdentifiers(itemNumbers->text());

        pCameraDialog->SetCameraInfo(&cameraInfo);

        if(QDialog::Accepted == pCameraDialog->exec()){
            updateCameraRowData(m_CurrentRow,cameraInfo.GetIPAddress(),cameraInfo.GetMACAddress(),cameraInfo.GetName(),cameraInfo.GetCameraIdentifiers());

            //m_objSeatDetection.ReplaceCameraInfo(m_CurrentRow,cameraInfo);
        }
        delete pCameraDialog;
    }
}
/**
 * @brief SeatParameterDialog::on_pushButtonMoveUpCamera_clicked
 * 上移一个相机
 */
void SeatParameterDialog::on_pushButtonMoveUpCamera_clicked()
{
    MoveUpRow(ui->tableWidgetCameras);
    //m_objSeatDetection.SwapCameraInfo(m_CurrentRow,m_CurrentRow - 1);
}
/**
 * @brief SeatParameterDialog::on_pushButtonMoveDownCamera_clicked
 * 下移一个相机
 */
void SeatParameterDialog::on_pushButtonMoveDownCamera_clicked()
{
    MoveDownRow(ui->tableWidgetCameras);
    //m_objSeatDetection.SwapCameraInfo(m_CurrentRow,m_CurrentRow + 1);
}

void SeatParameterDialog::on_pushButtonAddCategory_clicked()
{
    ClassDialog* pClassDialog = new ClassDialog(this);
    CategoryInfo categoryInfo;
    pClassDialog->SetCategoryInfo(&categoryInfo);

    if(QDialog::Accepted == pClassDialog->exec()){
        // 随机生成员工信息
        QString strName = categoryInfo.GetName();
        int     nKey = categoryInfo.GetKey();

        int rowCount = ui->tableWidgetClasses->rowCount();
        ui->tableWidgetClasses->setRowCount(rowCount + 1);

        QTableWidgetItem* item = new QTableWidgetItem(QString::number(nKey));
        item->setTextAlignment(Qt::AlignCenter);
        ui->tableWidgetClasses->setItem(rowCount, 0, item);

        item = new QTableWidgetItem(strName);
        item->setTextAlignment(Qt::AlignCenter);
        ui->tableWidgetClasses->setItem(rowCount, 1, item);

        m_objSeatDetection.AppendCategoryInfo(categoryInfo);
    }
    delete pClassDialog;
}

void SeatParameterDialog::on_pushButtonDeleteCategory_clicked()
{
    int currentRow = -1;
    // 获取当前选中的行
    QModelIndexList selectedIndexes = ui->tableWidgetClasses->selectionModel()->selectedIndexes();
    if (!selectedIndexes.isEmpty()) {
        currentRow = selectedIndexes.first().row();

        ui->tableWidgetClasses->removeRow(currentRow);

        m_objSeatDetection.RemoveCategoryInfo(currentRow);
    }
}

void SeatParameterDialog::on_pushButtonModifyCategory_clicked()
{
    int m_CurrentRow = ui->tableWidgetClasses->currentRow();//获取当前行的Index
    if(m_CurrentRow != -1){
        QTableWidgetItem *itemKey = ui->tableWidgetClasses->item(m_CurrentRow, 0);
        QTableWidgetItem *itemName = ui->tableWidgetClasses->item(m_CurrentRow, 1);

        ClassDialog* pClassDialog = new ClassDialog(this);
        CategoryInfo categoryInfo;
        categoryInfo.SetKey(itemKey->text().toInt());
        categoryInfo.SetName(itemName->text());

        pClassDialog->SetCategoryInfo(&categoryInfo);

        if(QDialog::Accepted == pClassDialog->exec()){
            QTableWidgetItem* pItem = new QTableWidgetItem(QString::number(categoryInfo.GetKey()));
            pItem->setTextAlignment(Qt::AlignCenter);
            ui->tableWidgetClasses->setItem(m_CurrentRow,0,pItem);

            pItem = new QTableWidgetItem(categoryInfo.GetName());
            pItem->setTextAlignment(Qt::AlignCenter);
            ui->tableWidgetClasses->setItem(m_CurrentRow,1,pItem);

            m_objSeatDetection.ReplaceCategoryInfo(m_CurrentRow,categoryInfo);
        }
        delete pClassDialog;
    }
}

void SeatParameterDialog::on_pushButtonMoveUpCategory_clicked()
{
    MoveUpRow(ui->tableWidgetClasses);
    m_objSeatDetection.SwapCategoryInfo(m_CurrentRow,m_CurrentRow - 1);
}

void SeatParameterDialog::on_pushButtonMoveDownCategory_clicked()
{
    MoveDownRow(ui->tableWidgetClasses);
    m_objSeatDetection.SwapCategoryInfo(m_CurrentRow,m_CurrentRow + 1);
}

/**
 * @brief SeatParameterDialog::on_pushButtonAddDevice_clicked
 *  添加设备
 */
void SeatParameterDialog::on_pushButtonAddDevice_clicked()
{
    QIcon iconDevice;
    iconDevice.addPixmap(QPixmap(":/icon/res/icon/device.png"), QIcon::Normal);
    NewDeviceDialog* pNewDeviceDialog = new NewDeviceDialog(this);
    if(pNewDeviceDialog->exec() == QDialog::Accepted){
        QListWidgetItem* pItem = new QListWidgetItem(pNewDeviceDialog->GetCurrentDevice());
        pItem->setIcon(iconDevice);
        ui->listWidgetDevices->addItem(pItem);

        Device *pDevice = (Device *) ObjectFactory::createObject(pItem->text().toLatin1(),this);
        pItem->setData(Qt::UserRole,QVariant::fromValue(pDevice));
        m_objSeatDetection.GetDescription().AddDevice(pDevice);

        InitComboBoxAllDeviceDatas();
    }
}

/**
 * @brief SeatParameterDialog::on_pushButtonDeleteDevice_clicked
 * 删除设备
 */
void SeatParameterDialog::on_pushButtonDeleteDevice_clicked()
{
    QListWidgetItem * pItem = ui->listWidgetDevices->currentItem();
    int r = ui->listWidgetDevices->row(pItem);

    QListWidgetItem* item = ui->listWidgetDevices->takeItem(r);
    ui->listWidgetDevices->removeItemWidget(item);
    delete item;

    m_objSeatDetection.GetDescription().RemoveDevice(ui->listWidgetDevices->currentRow());
}

void SeatParameterDialog::onImageWidthTextChanged(const QString &text){

}

void SeatParameterDialog::onImageHeightTextChanged(const QString &text){

}

void SeatParameterDialog::onImageChannelTextChanged(const QString &text){

}

void SeatParameterDialog::onPartWidthTextChanged(const QString &text){

}

void SeatParameterDialog::onPartHeightTextChanged(const QString &text){

}

void SeatParameterDialog::onSubwindowRowTextChanged(){

}

void SeatParameterDialog::onSubwindowColumnTextChanged(){

}

void SeatParameterDialog::onSubwindowTotalTextChanged(){
    QString strTotal = ui->spinBoxSubwindowTotal->text();
    RenewCameraFaceComboBoxs(strTotal.toInt());
}

void SeatParameterDialog::onRS232DeviceIDTextChanged(const QString &text){
    m_pCurrentDevice->setStrID(text);

    InitComboBoxAllDeviceDatas();
}

void SeatParameterDialog::onDahengCameraDeviceIDTextChanged(const QString &text){
    m_pCurrentDevice->setStrID(text);

    InitComboBoxAllDeviceDatas();
}

void SeatParameterDialog::onCamerasTextChanged(const QString& text){
    QVector<CameraInfo> camerainfos = m_objSeatDetection.GetCameraInfos();
    CameraInfo ci;
    for(int i = 0;i < camerainfos.count();i++){
        ci = camerainfos.at(i);
        if(ci.GetName() == ui->comboBoxCameras->currentText()){
            break;
        }
    }

    ui->lineEditDahengCameraDeviceIP->setText(ci.GetIPAddress());
    //
    ((DahengCameraDevice*)m_pCurrentDevice)->SetIP(ci.GetIPAddress());
}

void SeatParameterDialog::onDahengCameraDeviceIPTextChanged(const QString &text){
    ((DahengCameraDevice*)m_pCurrentDevice)->SetIP(text);
}

void SeatParameterDialog::onJakaRobotDeviceIDTextChanged(const QString &text){
    m_pCurrentDevice->setStrID(text);

    InitComboBoxAllDeviceDatas();
}

void SeatParameterDialog::onJakaRobotDeviceIPTextChanged(const QString &text){
    ((JakaRobotDevice*)m_pCurrentDevice)->SetIP(text);
}

void SeatParameterDialog::onJakaRobotDevicePortTextChanged(const QString &text){
    ((JakaRobotDevice*)m_pCurrentDevice)->SetPort_(text.toInt());
}

void SeatParameterDialog::onRecognizeDeviceIDTextChanged(const QString &text){
    m_pCurrentDevice->setStrID(text);

    InitComboBoxAllDeviceDatas();
}

void SeatParameterDialog::onRecognizeDeviceUseGPUTextChanged(const QString &text){
    QVariant tempValue = text;
    bool tempFinished = tempValue.toBool();
    qDebug() << text;
    ((RecognizeDevice*)m_pCurrentDevice)->SetUseGPU(tempFinished);
}

void SeatParameterDialog::onSerialPortPortTextChanged(const QString &text){
    qDebug() << text;
    QString strPort = ui->comboBoxPort->currentText();
    qDebug() << strPort;
    QStringList strList = strPort.split(" ");
    if(strList.size() >= 2){
        strPort = strList.at(0);
        strPort.replace("COM","");
    }

    ((RS232Device*)m_pCurrentDevice) ->SetSPort(strPort.toInt());
}

void SeatParameterDialog::onSerialPortBaudTextChanged(const QString &text){
    qDebug() << ((RS232Device*)m_pCurrentDevice)->GetBaud();

    QSerialPort::BaudRate br = (QSerialPort::BaudRate)text.toInt();
    ((RS232Device*)m_pCurrentDevice)->SetBaud(br);
}

void SeatParameterDialog::onSerialPortDatabitsTextChanged(const QString &text){
    qDebug() << text;
    ((RS232Device*)m_pCurrentDevice)->SetDatabits((QSerialPort::DataBits)text.toInt());
}

void SeatParameterDialog::onSerialPortStopbitsTextChanged(const QString &text){
    qDebug() << text;
    if(text == "1 位"){
        ((RS232Device*)m_pCurrentDevice)->SetStopbits(QSerialPort::StopBits::OneStop);
    }else if(text == "1.5 位"){
        ((RS232Device*)m_pCurrentDevice)->SetStopbits(QSerialPort::StopBits::OneAndHalfStop);
    }else if(text == "2 位"){
        ((RS232Device*)m_pCurrentDevice)->SetStopbits(QSerialPort::StopBits::TwoStop);
    }
}

void SeatParameterDialog::onSerialPortParityTextChanged(const QString &text){
    qDebug() << text;
    ((RS232Device*)m_pCurrentDevice)->SetParity((QSerialPort::Parity)text.toInt());
    if(text == "无奇偶校验"){
        ((RS232Device*)m_pCurrentDevice)->SetParity(QSerialPort::Parity::NoParity);
    }else if(text == "偶校验"){
        ((RS232Device*)m_pCurrentDevice)->SetParity(QSerialPort::Parity::EvenParity);
    }else if(text == "奇校验"){
        ((RS232Device*)m_pCurrentDevice)->SetParity(QSerialPort::Parity::OddParity);
    }else if(text == "Space"){
        ((RS232Device*)m_pCurrentDevice)->SetParity(QSerialPort::Parity::SpaceParity);
    }else if(text == "Mark"){
        ((RS232Device*)m_pCurrentDevice)->SetParity(QSerialPort::Parity::MarkParity);
    }
}

void SeatParameterDialog::onSerialPortFlowcontrolTextChanged(const QString &text){
    qDebug() << text;

}

void SeatParameterDialog::onDelayDurationTextChanged(const QString &text){
    qDebug() << text;
    ((DelayOperation*)m_pCurrentOperation)->setNDuration(text.toInt());
}

void SeatParameterDialog::onLEDChannelTextChanged(const QString &text){
    qDebug() << text;
    ((WordopControlLEDOperation*)m_pCurrentOperation)->setNChannel(text.toInt());
}

void SeatParameterDialog::onLEDValueTextChanged(const QString &text){
    qDebug() << text;
    ((WordopControlLEDOperation*)m_pCurrentOperation)->setNValue(text.toInt());
}

void SeatParameterDialog::onJoint1TextChanged(const QString &text){
    ((MoveJointOperation*)m_pCurrentOperation)->SetMoveJoint(0,text.toDouble());
}

void SeatParameterDialog::onJoint2TextChanged(const QString &text){
    ((MoveJointOperation*)m_pCurrentOperation)->SetMoveJoint(1,text.toDouble());
}

void SeatParameterDialog::onJoint3TextChanged(const QString &text){
    ((MoveJointOperation*)m_pCurrentOperation)->SetMoveJoint(2,text.toDouble());
}

void SeatParameterDialog::onJoint4TextChanged(const QString &text){
    ((MoveJointOperation*)m_pCurrentOperation)->SetMoveJoint(3,text.toDouble());
}

void SeatParameterDialog::onJoint5TextChanged(const QString &text){
    ((MoveJointOperation*)m_pCurrentOperation)->SetMoveJoint(4,text.toDouble());
}

void SeatParameterDialog::onJoint6TextChanged(const QString &text){
    ((MoveJointOperation*)m_pCurrentOperation)->SetMoveJoint(5,text.toDouble());
}

void SeatParameterDialog::onSnapImageTextChanged(const QString &text){
    ((SnapOperation*)m_pCurrentOperation)->setNImageNum(text.toInt());
}

void SeatParameterDialog::onRecognizeImageTextChanged(const QString &text){
    ((RecognizeOperation*)m_pCurrentOperation)->setNImageNum(text.toInt());
}

void SeatParameterDialog::onStepNumberTextChanged(const QString &text){
    ((Step*)m_pCurrentStep)->setStrID(text);
}

void SeatParameterDialog::onLEDDevicesTextChanged(const QString &text){
    m_pCurrentOperation->SetDeviceId(text);
}

void SeatParameterDialog::onMovelJointDevicesTextChanged(const QString &text){
    m_pCurrentOperation->SetDeviceId(text);
}

void SeatParameterDialog::onSnapDevicesTextChanged(const QString &text){
    m_pCurrentOperation->SetDeviceId(text);
}

void SeatParameterDialog::onRecognizeDevicesTextChanged(const QString &text){
    m_pCurrentOperation->SetDeviceId(text);
}

void SeatParameterDialog::on_pushButtonAddSquence_clicked()
{
    Sequence* pSequence = new Sequence();
    m_objSeatDetection.GetDescription().AddSequence(pSequence);

    QIcon iconSequence;
    iconSequence.addPixmap(QPixmap(":/icon/res/icon/sequence.png"), QIcon::Normal);

    QTreeWidgetItem *sequenceItem = new QTreeWidgetItem(QStringList() << ("Sequence"),QTreeWidgetItem::ItemType::UserType);
    ui->treeWidgetSequences->addTopLevelItem(sequenceItem);
    sequenceItem->setData(0,Qt::UserRole,QVariant::fromValue(pSequence));
    sequenceItem->setIcon(0,iconSequence);
    return;
}

void SeatParameterDialog::on_pushButtonDeleteSquence_clicked()
{
    int index = -1;
    QList<Sequence *> squences = m_objSeatDetection.GetDescription().GetSequences();
    for(int i = 0;i < squences.size();i++){
        if((void*) m_pCurrentSequence == (void*)squences.at(i)){
            index = i;
            break;
        }
    }

    if(index >= 0){
        m_objSeatDetection.GetDescription().RemoveSequence(index);
        QTreeWidgetItem * pItem = ui->treeWidgetSequences->currentItem();
        delete pItem;
    }
}

void SeatParameterDialog::OnTreeSequenceRightButtonClicked(QTreeWidgetItem* item, int column){
    if (qApp->mouseButtons() == Qt::RightButton) { // 只针对鼠标右键
        QTreeWidget* tree = item->treeWidget(); // 获取当前item所在的QTreeWidget
        QMenu* menu = new QMenu(tree);

        QAction* actionAdd = new QAction("添加一个节点");
        QAction* actionDelete = new QAction("删除当前节点");

        // 为右键菜单上的QAction创建信号槽，添加对应的功能
        connect(actionAdd, &QAction::triggered, this, [=] {
            if(m_eCurrentNodeType == NodeType::SequenceType){
                Step* pStep = (Step *) ObjectFactory::createObject("Step",m_pCurrentSequence);
                m_pCurrentSequence->AddStep(pStep);

                QIcon iconStep;
                iconStep.addPixmap(QPixmap(":/icon/res/icon/step.png"), QIcon::Normal);

                QTreeWidgetItem *sequenceItem = ui->treeWidgetSequences->currentItem();
                QTreeWidgetItem *stepItem = new QTreeWidgetItem(QStringList() << ("Step"),QTreeWidgetItem::ItemType::UserType);
                sequenceItem->addChild(stepItem);
                stepItem->setData(0,Qt::UserRole,QVariant::fromValue(pStep));
                stepItem->setIcon(0,iconStep);
            }else if(m_eCurrentNodeType == NodeType::StepType){
                QIcon iconOperation;
                iconOperation.addPixmap(QPixmap(":/icon/res/icon/operation.png"), QIcon::Normal);
                NewOperationDialog* pNewOperationDialog = new NewOperationDialog(this);
                QTreeWidgetItem* pStepItem = ui->treeWidgetSequences->currentItem();
                if(pNewOperationDialog->exec() == QDialog::Accepted){
                    QTreeWidgetItem* pOperationItem = new QTreeWidgetItem(QStringList() << (pNewOperationDialog->GetCurrentOperation()),QTreeWidgetItem::ItemType::UserType);
                    //ui->listWidgetDevices->addItem(pItem);
                    QIcon iconOperation;
                    iconOperation.addPixmap(QPixmap(":/icon/res/icon/operation.png"), QIcon::Normal);
                    Operation* pOperation = (Operation *) ObjectFactory::createObject(pNewOperationDialog->GetCurrentOperation().toLatin1(),m_pCurrentStep);
                    pOperationItem->setData(0,Qt::UserRole,QVariant::fromValue(pOperation));
                    pOperationItem->setIcon(0,iconOperation);
                    pStepItem->addChild(pOperationItem);

                    m_pCurrentStep->AddOperation(pOperation);
                }
            }
        });
        connect(actionDelete, &QAction::triggered, this, [=] {
            if(m_eCurrentNodeType == NodeType::StepType){
                Sequence* pSequence = (Sequence*)m_pCurrentStep->GetParent();
                QList<Step*> steps = pSequence->GetSteps();
                int index = -1;
                for(int i = 0; i < steps.size();i++){
                    if((void*)steps.at(i) == (void*)m_pCurrentStep){
                        index = i;
                    }
                }

                if(index >=  0){
                    pSequence->RemoveStep(index);

                    QTreeWidgetItem *stepItem = ui->treeWidgetSequences->currentItem();
                    delete stepItem;
                }
            }else if(m_eCurrentNodeType == NodeType::OperationType){
                Step* pStep = (Step*)m_pCurrentOperation->GetParent();
                QList<Operation*> operations = pStep->GetOperations();
                int index = -1;
                for(int i = 0; i < operations.size();i++){
                    if((void*)operations.at(i) == (void*)m_pCurrentOperation){
                        index = i;
                    }
                }

                if(index >=  0){
                    pStep->RemoveOperation(index);

                    QTreeWidgetItem *operationItem = ui->treeWidgetSequences->currentItem();
                    delete operationItem;
                }
            }
        });

        switch(m_eCurrentNodeType){
            case NodeType::SequenceType:
                menu->addAction(actionAdd);
                menu->addAction(actionDelete);
                break;

            case NodeType::StepType:
                menu->addAction(actionAdd);
                menu->addAction(actionDelete);
                break;

            case NodeType::OperationType:
                menu->addAction(actionDelete);
                break;
        }

        // 右键菜单在鼠标点击的位置显示
        menu->exec(QCursor::pos());
    }
}

void SeatParameterDialog::accept(){
    // (1) 图像地址
    m_objSeatDetection.GetBaseInfo().SetImageSrcPath(ui->lineEditBaseSrcPath->text());

    // (2) 图像 宽度、高度、通道
    m_objSeatDetection.GetImageInfo().SetImageWidth(ui->spinBoxImageWidth->text().toInt());
    m_objSeatDetection.GetImageInfo().SetImageHeight(ui->spinBoxImageHeight->text().toInt());
    m_objSeatDetection.GetImageInfo().SetImageChannel(ui->spinBoxImageChannel->text().toInt());

    // (3) 图像分割块尺寸
    m_objSeatDetection.GetCnnInfo().SetPartWidth(ui->spinBoxPartWidth->text().toInt());
    m_objSeatDetection.GetCnnInfo().SetPartHeight(ui->spinBoxPartHeight->text().toInt());

    // (4) 显示子图像分割数目
    m_objSeatDetection.GetDetectionWindowInfo().SetImageRow(ui->spinBoxSubwindowRow->text().toInt());
    m_objSeatDetection.GetDetectionWindowInfo().SetImageColumn(ui->spinBoxSubwindowColumn->text().toInt());
    m_objSeatDetection.GetDetectionWindowInfo().SetImageTotal(ui->spinBoxSubwindowTotal->text().toInt());

    // (5) 相机列表保存
    QVector<CameraInfo>& camerainfos = m_objSeatDetection.GetCameraInfos();
    for(int i = camerainfos.count() - 1; i >=0; i--){
        m_objSeatDetection.RemoveCameraInfo(i);
    }

    int nRowCount = ui->tableWidgetCameras->rowCount();
    QTableWidgetItem* pItem;
    for(int i = 0; i < nRowCount; i++){
        CameraInfo* pCameraInfo = new CameraInfo();
        pItem = ui->tableWidgetCameras->item(i,0);
        pCameraInfo->SetIPAddress(pItem->text());// IP

        pItem = ui->tableWidgetCameras->item(i,1);
        pCameraInfo->SetMACAddress(pItem->text());// MAC

        pItem = ui->tableWidgetCameras->item(i,2);
        pCameraInfo->SetName(pItem->text());// Name

        pItem = ui->tableWidgetCameras->item(i,3);
        pCameraInfo->SetCameraIdentifiers(pItem->text());// Identifiers

        m_objSeatDetection.AppendCameraInfo(*pCameraInfo);
    }

    // (6) 分类类别
    QVector<CategoryInfo>& categoryInfos = m_objSeatDetection.GetCategoryInfos();
    for(int i = categoryInfos.count() - 1; i >=0; i--){
        m_objSeatDetection.RemoveCategoryInfo(i);
    }

    nRowCount = ui->tableWidgetClasses->rowCount();
    for(int i = 0; i < nRowCount; i++){
        CategoryInfo* pCategoryInfo = new CategoryInfo();
        pItem = ui->tableWidgetClasses->item(i,0);
        pCategoryInfo->SetKey(pItem->text().toInt());// Key

        pItem = ui->tableWidgetClasses->item(i,1);
        pCategoryInfo->SetName(pItem->text());// Name

        m_objSeatDetection.AppendCategoryInfo(*pCategoryInfo);
    }

    // (7) 座位显示面
    QVector<SubImageInfo*> seatfaces = m_objSeatDetection.GetDetectionWindowInfo().GetSeatFaces();

    nRowCount = ui->tableWidgetDetectingWindow->rowCount();
    int nTotal = ui->spinBoxSubwindowTotal->text().toInt();
    m_objSeatDetection.GetDetectionWindowInfo().ResizeSeatFaces(nTotal);

    for(int i = 0;i < nRowCount;i++){
        QComboBox* pComboBoxCamera = (QComboBox*)ui->tableWidgetDetectingWindow->cellWidget(i,1);
        QComboBox* pComboBoxFace = (QComboBox*)ui->tableWidgetDetectingWindow->cellWidget(i,2);
        m_objSeatDetection.GetDetectionWindowInfo().SetSeatFaceContent(i,
                                                                                                pComboBoxFace->currentText(),
                                                                                                pComboBoxCamera->currentText());
    }

    // (8) 设备
    /*nRowCount = ui->listWidgetDevices->count();
    int nDeviceCount = m_objSeatDetection.GetDescription().GetDevices().count();
    for(int i = nDeviceCount - 1;i >= 0;i--){
        m_objSeatDetection.GetDescription().RemoveDevice(i);
    }

    for(int i = 0; i < nRowCount;i++){
        QListWidgetItem* pItem = ui->listWidgetDevices->item(i);
        Device *pDevice = (Device *) ObjectFactory::createObject(pItem->text().toLatin1(),this);

        pItem->setData(Qt::UserRole,QVariant::fromValue(pDevice));
        m_objSeatDetection.GetDescription().AddDevice(pDevice);
    }*/


    QDialog::accept();
}

